revision.js ➔ getVersions   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 11
rs 9.4285
nop 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A revision.js ➔ ... ➔ ??? 0 5 2
1
import path from 'path'
2
3
import {
4
  config,
5
  cmsData,
6
  coreUtils,
7
  Manager
8
} from '../../'
9
10
export function getFilesRevision(urls, fileName) {
11
  var res = []
12
  var number = 1
13
  var tplUrl = cmsData.file.fromUrl(fileName)
14
  fileName = fileName.split('/')
15
  fileName = fileName[fileName.length - 1]
16
  var publishDate = new Date()
17
  var json = null
18
19
  if(coreUtils.file.exist(tplUrl.publish.json)) {
20
    json = cmsData.file.get(tplUrl.publish.json)
21
    if(json != null && json[config.meta.name] != null) {
22
      publishDate = new Date(json[config.meta.name].latest.date)
23
    }
24
  }
25
26
  var publishVersion = false
27
  urls.forEach(function (urlObj) {
28
    var fileData = cmsData.fileAttr.get(urlObj.cleanPath)
29
    if(fileData.s === 'd' && cmsData.fileAttr.delete(urlObj.cleanPath) == cmsData.fileAttr.delete(fileName)) {
30
      var currentDate = new Date(urlObj.date)
31
      if(currentDate.getTime() > publishDate.getTime()) {
32
        if(!publishVersion && res[res.length - 1] != null) {
33
          res[res.length - 1].publishedDate = 'same'
34
        }
35
        publishVersion = true
36
        urlObj.publishedDate = 'after'
37
        
38
      }else if(currentDate.getTime() === publishDate.getTime()) {
39
        urlObj.publishedDate = 'same'
40
        publishVersion = true
41
      }else {
42
        urlObj.publishedDate = 'before'
43
      }
44
      urlObj.version = number
45
      number = number + 1
46
47
      var tplUrlObj = cmsData.file.fromUrl(urlObj.path)
48
      if(coreUtils.file.exist(tplUrlObj.publish.json)) {
49
        var jsonObj = cmsData.file.get(tplUrlObj.publish.json)
50
        urlObj[config.meta.name] = jsonObj[config.meta.name]
51
      }
52
      res.push(urlObj)
53
    }
54
  })
55
  return res
56
}
57
58
/**
59
 * Create and array of doc file path containing the versions of this doc
60
 * @param  {String} path of a doc
0 ignored issues
show
Documentation introduced by
The parameter path does not exist. Did you maybe forget to remove this comment?
Loading history...
61
 * @return {Array} the versions of the doc
62
 */
63
export function getVersions(docPath) {
64
  var result = []
65
  var files = Manager.instance.getList()
66
  var dataFile = path.join(config.data.url, docPath.replace('.' + config.files.templates.extension, '.json'))
67
  Array.prototype.forEach.call(files, (file) => {
68
    if (file.path.indexOf(dataFile) > -1) {
69
      result = file.revisions
70
    }
71
  })
72
  return result
73
}
74
75
/**
76
 * Return the revision from document html file path
77
 * if the docPath contains abe revision [status]-[date] will try to return this revision
78
 * else it will return the latest revision
79
 * or null
80
 * 
81
 * @param  {String} html path
0 ignored issues
show
Documentation introduced by
The parameter html does not exist. Did you maybe forget to remove this comment?
Loading history...
82
 * @return {Object} file revision | null
83
 */
84
export function getDocumentRevision(docPath) {
85
  var result = null
86
  var documentPath = docPath
87
  var latest = true
88
89
  if(cmsData.fileAttr.test(documentPath)){
90
    latest = false
91
    documentPath = cmsData.fileAttr.delete(documentPath)
92
  }
93
  var revisions = getVersions(documentPath)
94
  if (latest && revisions.length >= 0) {
95
    result = revisions[0]
96
  } else if (!latest) {
97
    Array.prototype.forEach.call(revisions, (revision) => {
98
      if (revision.html === docPath) {
99
        result = revision
100
      }
101
    })
102
    if (result === null && revisions.length >= 0) {
103
      result = revisions[0]
104
    }
105
  }
106
107
  return result
108
}
109
110
export function getStatusAndDateToFileName(date) {
111
  var res = date.substring(0, 4) + '-'
112
            + date.substring(4, 6) + '-'
113
            + date.substring(6, 11) + ':'
114
            + date.substring(11, 13) + ':'
115
            + date.substring(13, 15) + '.'
116
            + date.substring(15, 19)
117
  return res
118
}
119
120
export function removeStatusAndDateFromFileName(date) {
121
  return date.replace(/[-:\.]/g, '')
122
}
123
124
export function filePathInfos(pathFolder) {
125
  var pathArr = pathFolder.split('/')
126
  var name = pathArr[pathArr.length - 1]
127
128
  var rootArr = config.root.split('/')
129
  var website = rootArr[pathArr.length - 1]
130
  return {
131
    'name': name,
132
    'path': pathFolder,
133
    'website': website,
134
    'cleanPath': pathFolder.replace(config.root, '').replace(/\/$/, ''),
135
    'type': 'folder'
136
  }
137
}
138
139
export function mergeRevisions(files) {
140
  let merged = {}
141
  
142
  Array.prototype.forEach.call(files, (file) => {
143
    const parentRelativePath = file.parentRelativePath
144
145
    if(merged[parentRelativePath] == null) {
146
      merged[parentRelativePath] = {
147
        name: cmsData.fileAttr.delete(file.name),
148
        path: cmsData.fileAttr.delete(file.path),
149
        html: cmsData.fileAttr.delete(path.join('/', file.filePath.replace(/\.json/, `.${config.files.templates.extension}`))),
150
        htmlPath: path.join(config.root, config.publish.url, path.join('/', cmsData.fileAttr.delete(file.filePath.replace(/\.json/, `.${config.files.templates.extension}`)))),
151
        cleanPath: file.cleanPath,
152
        cleanName: file.cleanName,
153
        parentRelativePath: file.parentRelativePath,
154
        filePath: cmsData.fileAttr.delete(file.filePath),
155
        revisions: []
156
      }
157
    }
158
    merged[parentRelativePath].revisions.push(JSON.parse(JSON.stringify(file)))
159
  })
160
161
  return merged
162
}
163
164
export function sortRevisions(merged) {
165
  let sortedArray = []
166
167
  Array.prototype.forEach.call(Object.keys(merged), (key) => {
168
    let revisions = merged[key].revisions
169
    revisions.sort(coreUtils.sort.predicatBy('date', -1))
170
    merged[key].revisions = revisions
171
172
    if(revisions[0] != null) {
173
      merged[key].date = revisions[0].date
174
      merged[key].cleanDate = revisions[0].cleanDate
175
      merged[key].duration = revisions[0].duration
176
      merged[key].abe_meta = revisions[0].abe_meta
177
    }
178
179
    let newStatuses = []
180
    Array.prototype.forEach.call(revisions, (revision) => {
181
      const revisionStatus = revision.abe_meta.status
182
183
      if(typeof revisionStatus !== 'undefined' && revisionStatus !== null && revisionStatus != '') {
184
        if(!(revisionStatus in newStatuses)) {
185
          if (revisionStatus === 'publish') {
186
            merged[key][revisionStatus] = revision
187
          }else {
188
            merged[key][revisionStatus] = {}
189
          }
190
          merged[key][revisionStatus].path = revision.path
191
          merged[key][revisionStatus].html = revision.html
192
          merged[key][revisionStatus].htmlPath = revision.htmlPath
193
          merged[key][revisionStatus].date = revision.date
194
          merged[key][revisionStatus].cleanDate = revision.cleanDate
195
          merged[key][revisionStatus].link = revision.abe_meta.link
196
197
          newStatuses[revisionStatus] = true
198
        }
199
      }
200
    })
201
202
    sortedArray.push(merged[key])
203
  })
204
205
  return sortedArray
206
}
207
208
export function getFilesMerged(files) {
209
  const revisions = cmsData.revision.mergeRevisions(files)
210
211
  return cmsData.revision.sortRevisions(revisions)
212
}
213